/***
 * $HeadURL: http://gstestbbs.googlecode.com/svn/trunk/sfmforum/src/main/java/com/sfmforum/core/log/SfmForumLogger.java $
 * 
 **/
package com.sfmforum.core.log;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sfmforum.core.log.util.SfmForumLoggerResourcesLoader;

/***
 * log using properties<Key, Value> , if no properties file found, use normal
 * log mode.
 * 
 * @author $Author: nathanliaogs@gmail.com $
 * @version $Revision: 21 $
 * @modified $Date: 2012-03-25 15:39:36 +0000 (Sun, 25 Mar 2012) $
 * 
 **/
public class SfmForumLogger {
	private Log log;
	private Properties properties;
	private Locale locale;

	/**
	 * @param calzz
	 */
	private SfmForumLogger(Class calzz) {
		log = LogFactory.getLog(calzz);
		locale = Locale.getDefault();
		properties = SfmForumLoggerResourcesLoader.getLoggerProperties(locale);
	}

	/**
	 * @param calzz
	 * @param locale
	 */
	private SfmForumLogger(Class calzz, Locale locale) {
		log = LogFactory.getLog(calzz);
		this.locale = locale;
		properties = SfmForumLoggerResourcesLoader.getLoggerProperties(locale);
	}

	/**
	 * @param clazz
	 * @return
	 */
	public static SfmForumLogger getLogger(Class clazz) {
		return new SfmForumLogger(clazz);
	}

	public static SfmForumLogger getLogger(Class clazz, Locale locale) {
		return new SfmForumLogger(clazz, locale);
	}

	public void debug(String key, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.debug(MessageFormat.format(text, arguments));
		} else {
			log.debug(MessageFormat.format(key, arguments));
		}
	}

	public void debug(String key, Throwable t, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.debug(MessageFormat.format(text, arguments), t);
		} else {
			log.debug(MessageFormat.format(key, arguments), t);
		}
	}

	public void info(String key, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.info(MessageFormat.format(text, arguments));
		} else {
			log.info(MessageFormat.format(key, arguments));
		}
	}

	public void info(String key, Throwable t, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.info(MessageFormat.format(text, arguments), t);
		} else {
			log.info(MessageFormat.format(key, arguments), t);
		}
	}

	public void error(String key, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.error(MessageFormat.format(text, arguments));
		} else {
			log.error(MessageFormat.format(key, arguments));
		}
	}

	public void error(String key, Throwable t, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.error(MessageFormat.format(text, arguments), t);
		} else {
			log.error(MessageFormat.format(key, arguments), t);
		}
	}

	public void warn(String key, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.warn(MessageFormat.format(text, arguments));
		} else {
			log.warn(MessageFormat.format(key, arguments));
		}
	}

	public void warn(String key, Throwable t, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.warn(MessageFormat.format(text, arguments), t);
		} else {
			log.warn(MessageFormat.format(key, arguments), t);
		}
	}

	public void fatal(String key, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.fatal(MessageFormat.format(text, arguments));
		} else {
			log.fatal(MessageFormat.format(key, arguments));
		}
	}

	public void fatal(String key, Throwable t, Object... arguments) {

		String text = properties.getProperty(key);
		if (text != null) {
			log.fatal(MessageFormat.format(text, arguments), t);
		} else {
			log.fatal(MessageFormat.format(key, arguments), t);
		}
	}

}
